﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Maticsoft.Web.sysMan
{
    public partial class AddNews : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindDDL();
                if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                {
                    int newsId = (Convert.ToInt32(Request.Params["id"]));
                    ShowInfo(newsId);
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            if (string.IsNullOrEmpty(this.txtnewsPubTime.Text ))
            {
                Maticsoft.Common.MessageBox.Show(this, "请填写发布时间！");

                return;
            }

            string newsContent = this.txtnewsContent.Text;
            string newsTitle = this.txtnewsTitle.Text;
            string newsType = this.ddl_type.SelectedItem.Value;
            string newsPublisher = this.txtnewsPublisher.Text;
            DateTime newsPubTime = DateTime.Parse(this.txtnewsPubTime.Text);

            Maticsoft.Model.newsInfo model = new Maticsoft.Model.newsInfo();
            model.newsContent = newsContent;
            model.newsTitle = newsTitle;
            model.newsType = newsType;
            model.newsPublisher = newsPublisher;
            model.newsPubTime = newsPubTime;

            Maticsoft.BLL.newsInfo bll = new Maticsoft.BLL.newsInfo();

            if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
            {
                model.newsId = int.Parse(this.lblnewsId.Text);
                model.newsCount = int.Parse(this.txtnewsCount.Text);

                if (this.ddl_type.SelectedValue == "资料库")
                {
                    model.parentId = int.Parse(this.ddl_parent.SelectedValue);
                }

                if (bll.Update(model))
                {
                    Maticsoft.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "ListNews.aspx");
                }

            }
            else 
            {
                model.newsCount = 0;

                if (this.ddl_type.SelectedValue == "资料库")
                {
                    model.parentId = int.Parse(this.ddl_parent.SelectedValue);
                }

                if (bll.Add(model) > 0)
                {
                    Maticsoft.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "ListNews.aspx");
                }
            }
           
            
        }

        private void ShowInfo(int newsId)
        {
            Maticsoft.BLL.newsInfo bll = new Maticsoft.BLL.newsInfo();
            Maticsoft.Model.newsInfo model = bll.GetModel(newsId);
            this.lblnewsId.Text = model.newsId.ToString();
            this.txtnewsContent.Text = model.newsContent;
            this.txtnewsTitle.Text = model.newsTitle;
            this.ddl_type.SelectedValue = model.newsType;
            this.txtnewsPublisher.Text = model.newsPublisher;
            this.txtnewsPubTime.Text = model.newsPubTime.ToString();
            this.txtnewsCount.Text = model.newsCount.ToString();
            this.ddl_parent.SelectedValue = model.parentId.ToString();

        }


        //添加资料
        protected void btn_addZL_Click(object sender, EventArgs e)
        {
            Maticsoft.Model.newsInfo n = new Maticsoft.Model.newsInfo();
            Maticsoft.BLL.newsInfo nm = new BLL.newsInfo();

            n.newsPubTime = DateTime.Now;
            n.parentId = 0;
            n.newsTitle = this.txt_zlk.Text;
            n.newsType = "资料库";

            if (nm.Add(n) > 0)
            {
                BindDDL();
            }


        }

        private void BindDDL()
        {
            Maticsoft.BLL.newsInfo nm = new BLL.newsInfo();

            DataSet ds = nm.GetList(" parentId = 0 ");

            this.ddl_parent.DataSource = ds;
            this.ddl_parent.DataTextField = "newsTitle";
            this.ddl_parent.DataValueField = "newsId";
            this.ddl_parent.DataBind();

        }

        protected void ddl_type_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.ddl_type.SelectedValue != "资料库")
            {
                this.txt_zlk.Enabled = false;
                this.btn_addZL.Enabled = false;
                this.ddl_parent.Enabled = false;
            }
            else
            {
                this.txt_zlk.Enabled = true;
                this.btn_addZL.Enabled = true;
                this.ddl_parent.Enabled = true;
            }
        }
    }
}